package com.wsc.wisdomchina.activitity.introduction.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.JuWeiInfoDetailActivity;
import com.wsc.wisdomchina.adapter.JWXX_Adapter;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.javaBean.CommitteeNewsListBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.List;

/**
 * 社区信息
 */
public class CommunityInformationFragment extends Fragment implements ContributorView {
    private PullToRefreshListView fragment_listView;//列表
    private Context context;
    private ListView actualListView;
    private ContributorPresenter contributorPresenter;
    private final String BUILDING_ACTIVITY_LIST_FLAG = "BUILDING_ACTIVITY_LIST_FLAG";
    private List<CommitteeNewsListBean> floorActivityList;
    private JWXX_Adapter floorInoAdapter;
    private String id;

    public static CommunityInformationFragment newInstance(Bundle b) {
        CommunityInformationFragment fragment = new CommunityInformationFragment();
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        contributorPresenter = new ContributorPresenter(this, getActivity());
        id = getArguments().getString("id");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_community_information, container, false);
        initView(inflate);
        showData();
        return inflate;
    }

    /**
     * 初始化控件
     *
     * @param inflate
     */
    private void initView(View inflate) {
        fragment_listView = ((PullToRefreshListView) inflate.findViewById(R.id.fragment_listView));
        fragment_listView.setMode(PullToRefreshBase.Mode.DISABLED);
        actualListView = fragment_listView.getRefreshableView();

        actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        actualListView.setDivider(null);
        Tools.showRefreshView(fragment_listView);

        actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (floorActivityList != null && floorActivityList.size() > 0) {
                    Intent intent = new Intent(context, JuWeiInfoDetailActivity.class);
                    intent.putExtra("id", floorActivityList.get(position - 1).getId());
                    intent.putExtra("name", floorActivityList.get(position - 1).getName());
                    startActivity(intent);
                }
            }
        });
    }

    private void showData() {
        contributorPresenter.getJuWeiInfoList(id, BUILDING_ACTIVITY_LIST_FLAG);
    }

    @Override
    public void onLoadContributorStart(String flag) {
    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
//        try {
        if (flag.equals(BUILDING_ACTIVITY_LIST_FLAG)) {
            RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
            String code = respObj.getCode();
            Object object = respObj.getMessage();
            if (code.equals("200") && object != null) {
                String jsonMessage = JsonTools.objectToJsonString(object);
                List<CommitteeNewsListBean> floorActivityListBeen = JsonTools.jsonStringToList(jsonMessage, CommitteeNewsListBean.class);
                if (floorActivityListBeen != null && floorActivityListBeen.size() > 0) {
                    floorActivityList = floorActivityListBeen;
                    floorInoAdapter = new JWXX_Adapter(context, floorActivityList);
                    actualListView.setAdapter(floorInoAdapter);
                }

            }
        }
//        } catch (Exception e) {
//        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
        fragment_listView.onRefreshComplete();
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }
}
